package com.mobile.web.supplementInfo;

import com.mobile.service.dto.SupplementInfoDTO;
import com.mobile.service.dto.SupplementInfoQueryDTO;
import com.mobile.service.inter.SupplementInfoService;
import com.mobile.util.*;
import com.mobile.web.base.BaseController;
import com.mobile.util.config.aop.AutoFill;
import com.mobile.util.config.aop.OperateType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 补正信息表API接口
 */
@RestController
@RequestMapping("/supplementInfo")
@Tag(name = "补正信息管理接口", description = "包含补正信息的查询、新增、修改、删除等操作")
public class SupplementInfoController extends BaseController {

    @Autowired
    private SupplementInfoService supplementInfoService;

    @PostMapping("/addSupplementInfo")
    //@RequiresPermissions("supplementInfomana:add:1")
    @Operation(summary = "创建补正信息")
    @AutoFill(OperateType.INSERT)
    public Object addSupplementInfo(@Valid @RequestBody SupplementInfoDTO dto) {
        Map<String, Object> map;
        try {
            map = supplementInfoService.addSupplementInfo(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getSupplementInfoListPage")
    //@RequiresPermissions("supplementInfomana:read:1")
    @Operation(summary = "分页查询补正信息")
    public Object getSupplementInfoListPage(@Validated SupplementInfoQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageResult<SupplementInfoDTO> page = supplementInfoService.getSupplementInfoListPage(queryDTO);
            map.put("page", page);
            map.put("data", page.getPageDatas());
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getSupplementInfoById")
    //@RequiresPermissions("supplementInfomana:edit:1")
    @Operation(summary = "查询补正信息详情")
    public Object getSupplementInfoById(
        @Parameter(description = "补正信息主键", required = true) 
        @RequestParam("supplementInfoId") Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            SupplementInfoDTO dto = supplementInfoService.getSupplementInfoById(id);
            if (StringUtil.isEmpty(dto)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            } else {
                map.put("data", dto);
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            }
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @PostMapping("/updateSupplementInfo")
    //@RequiresPermissions("SupplementInfomana:edit:1")
    @Operation(summary = "编辑补正信息")
    @AutoFill(OperateType.UPDATE)
    public Object updateSupplementInfo(@Valid SupplementInfoDTO dto) {
        Map<String, Object> map;
        try {
            map = supplementInfoService.updateSupplementInfo(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/removeSupplementInfo")
    //@RequiresPermissions("supplementInfomana:delete:1")
    public Object removeSupplementInfo(@RequestParam("supplementInfoId") Integer id) {
        Map<String, Object> map;
        try {
            map = supplementInfoService.removeSupplementInfo(id);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/getSupplementInfoList")
    //@RequiresPermissions("supplementInfomana:read:1")
    @Operation(summary = "条件查询补正信息列表")
    public Object getSupplementInfoList(@Validated SupplementInfoQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<SupplementInfoDTO> list = supplementInfoService.getSupplementInfoList(queryDTO);
            map.put("data", list);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
}
